*********************************************************************************************************
*									 Reproduction do-file for 											*
*								Steiner, Nils D. & Landwehr, Claudia  									*
*									Learning the Brexit lesson?											*
*	 Shifting support for direct democracy in Germany in the aftermath of the Brexit referendum 		*
*********************************************************************************************************

* This do file reproduces all results reported in the paper and the appendix

* How to reproduce the results: 
* 1. Obtain the survey data from GESIS at https://doi.org/10.4232/1.13129
* 2. Run the code below


******************************** Settings **************************************

* install ados and packages use
ssc install fre
ssc install ciplot
ssc install estout
ssc install coefplot
ssc install mplotoffset
ssc install blindschemes

* set settings
clear all
version 17.0
set maxvar 20000
set scheme plottig



******************************** Load data *************************************
**** Dataset used: GLES (2018). Repeatedly questioned respondents of the Short-term Campaign Panel 2013 and 2017 (GLES). GESIS Data Archive, Cologne. ZA6827 Data file Version 1.0.0, https://doi.org/10.4232/1.13129.

* Load dataset 
use "ZA6827_v1-0-0.dta"

* Process missing values (do-file is provided by GESIS together with the dataset)
do "ZA6827_missing.do"



******************************** Recoding ***************************************

* Referendum support: "Citizens should be able to initiate binding referendums at the federal level."
	* availbale in waves: 2, 5, 8, 9, 10, 13, 16
	recode kp2_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_July13)
	recode kp5_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_Sept13)
	recode kp8_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_Oct14)
	recode kp9_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_Oct15)
	recode kp10_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_Oct16)
	recode kp13_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_Jul17)
	recode kp16_2880e (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(ref_Sept17)
	
	label variable ref_July13 "July 2013"
	label variable ref_Sept13 "Sept 2013"
	label variable ref_Oct14 "Oct 2014"
	label variable ref_Oct15 "Oct 2015"
	label variable ref_Oct16 "Oct 2016"
	label variable ref_Jul17 "July 2017"
	label variable ref_Sept17 "Sept 2017"
	
	* create first difference variable for wave 10 (Oct 2016) vs. wave 9 (Oct 2015) -> outcome variable in main regression analysis
	gen ref_diff16vs15=ref_Oct16-ref_Oct15
	
	
* Attitudes towards European integration: "Should European unification be pushed further to establish a common European government soon or is European unification already going too far?"
	* wave 6
	fre kp6_1250
	recode kp6_1250 (1=6 "More EU integration") (2=5) (3=4) (4=3) (5=2) (6=1) (7=0 "Less EU integration"), gen(EU_w6)

	recode EU_w6 (6 5 4=2 "More EU integration") (3=1) (2 1 0=0 "Less EU integration"), gen(EU_trich_w6)
		tab EU_w6 EU_trich_w6

	* wave 10
	fre kp10_1250
	recode kp10_1250 (1=6 "More EU integration") (2=5) (3=4) (4=3) (5=2) (6=1) (7=0 "Less EU integration"), gen(EU_w10)

	
*** Voting intention (all waves)
	* change label for "other party"
	foreach i of numlist 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 {
	label define kp`i'_190ba 801 "other party", modify
	}	
	 
	foreach i of numlist 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 {
	clonevar voteint_w`i'=kp`i'_190ba
	}
	
	* recodings for vote intention in wave 9
		* label variable
		recode voteint_w9 (808=.)
		* replace with zero for those who do (probably) not intend to vote (-> these were not asked about a party and would otherwise have missing values)
		replace voteint_w9=0 if kp9_170==4 |  kp9_170==5
	

*** Political interest (self-reported) (wave 9)
	recode kp9_010 (5 4 3=0 "pol. interest low")(1 2=1 "pol. interest high"), gen(polint_w9)

	
*** Political knowledge based on quiz items (wave 7)	
	* code dummy variables for correct answers
	recode kp7_110 (1 3 .a .b=0) (2=1), gen(kp7_110_korrekt)
	recode kp7_090_v1 (0 .a .b=0) (1=1), gen(kp7_090_v1_korrekt)
	recode kp7_130 (1 2 4 .a .b=0) (3=1), gen(kp7_130_korrekt)
	recode kp7_3430d (2 3 4 6 7 215 322 .a .b=0) (5=1), gen(kp7_3430d_korrekt)
	recode kp7_3430f (2 3 4 5 6 215 322 .a .b=0) (7=1), gen(kp7_3430f_korrekt)
	recode kp7_3430a (1 3 4 5 6 7 215 322 .a .b=0) (2=1), gen(kp7_3430a_korrekt)
	recode kp7_3430e (2 3 4 5 7 215 322 .a .b=0) (6=1), gen(kp7_3430e_korrekt)
	recode kp7_3430c (2 4 5 6 7 215 322 .a .b=0) (3=1), gen(kp7_3430c_korrekt)
	recode kp7_3430b (2 3 5 6 7 215 322 .a .b=0) (4=1), gen(kp7_3430b_korrekt)
	
	* compute the sum of correct answers
	generate polknowlegde_w7=kp7_110_korrekt+kp7_090_v1_korrekt+kp7_130_korrekt+kp7_3430d_korrekt+kp7_3430f_korrekt+kp7_3430a_korrekt+kp7_3430e_korrekt+kp7_3430c_korrekt+kp7_3430b_korrekt
	
	* check the distribution of the variable
	fre polknowlegde_w7
	
	* code into a dummy variable for political knowledge
	recode polknowlegde_w7 (0/7=0) (8 9=1), gen(polknowlegde_w7_dummy)

	
*** Attitudes towards immigration (wave 10)
		* "Should immigration opportunities for foreigners be facilitated or restricted?"
	recode kp9_1130 (11=1 "restrict immigration") (10=2) (9=3) (8=4) (7=5) (6=6) (5=7) (4=8) (3=9) (2=10) (1=11 "facilitate immigration"), gen(immigration_w9)
	fre immigration_w9

	
*** Sociodemographics
	* Income 
	fre kp1_2591
	clonevar inc_raw=kp1_2591
	recode inc_raw (1 2 3 = 1 "income <1000€") (4 5 6 = 2 "income 1000-2000€") (7 8 = 3 "income 2000-3000€") (9 = 4 "income 3000-4000€") (10 11 12 13 14 = 5 "income >4000€"), gen(incomecat)	
	
	* Age (in 2016)
	gen age=2016-kpx_2290
	recode age (16/34=0 "up to 34") (35/49=1 "35 to 49") (46/64=2 "50 to 64") (65/120=3 "65+"), gen(agegroup)

	* Gender
	fre kpx_2280
	recode kpx_2280 (2 = 1 "female") (1 = 0 "male"), gen(female)

	* State of residence:  East vs. West
	recode ostwest13 (-95 = .d) (0 = 1 "East Germany") (1 = 0 "West Germany"), gen(east)
	
	* Education
	fre kp10_2320
	recode kp10_2320 (1 2 = 1) (3 = 2) (4 5 = 3) (9 = .), gen(educat)
	label define educat ///
	1 "education low" ///
	2 "education middle"  ///
	3 "education high"
	label values educat educat


	
	
******************************** Descriptives **********************************
			
***** FIGURE 1: Referendum support over time
	* Look at the means in referendum support over time
		* Note: ciplot excludes listwise; thus only respondents who provided answers in all waves are included
	* For all
		ciplot ref_July13 ref_Sept13 ref_Oct14 ref_Oct15 ref_Oct16 ref_Jul17 ref_Sept17, ///
			msymbol(O O) mcolor(black black black black orange_red orange_red orange_red) mfcolor(black black) ///
			xlabel(, angle(045)) ///
			note("n=1,237", pos(5)) xtitle(" ", size(zero)) ///
			title("All respondents") ///
			name(gr1, replace) nodraw
		
	* By attitude towards European integration 	
		ciplot ref_July13 ref_Sept13 ref_Oct14 ref_Oct15 ref_Oct16 ref_Jul17 ref_Sept17 if EU_trich_w6==0, ///
			msymbol(O O) mcolor(black black black black orange_red orange_red orange_red) mfcolor(black black) ///
			xlabel(, angle(045)) ///
			note("n=609", pos(5)) xtitle(" ", size(zero)) ///
			title("Less EU integration") ///
			name(gr2, replace) nodraw
		
		ciplot ref_July13 ref_Sept13 ref_Oct14 ref_Oct15 ref_Oct16 ref_Jul17 ref_Sept17 if EU_trich_w6==1, ///
			msymbol(O O) mcolor(black black black black orange_red orange_red orange_red) mfcolor(black black) ///
			note("n=297", pos(5)) xtitle(" ", size(zero)) ///
			xlabel(, angle(045)) ///
			title("Neither") ///
			name(gr3, replace) nodraw
		
		ciplot ref_July13 ref_Sept13 ref_Oct14 ref_Oct15 ref_Oct16 ref_Jul17 ref_Sept17 if EU_trich_w6==2, ///
			msymbol(O O) mcolor(black black black black orange_red orange_red orange_red) mfcolor(black black) ///
			note("n=331", pos(5)) xtitle(" ", size(zero)) ///
			xlabel(, angle(045)) ///
			title("More EU integration") ///
			name(gr4, replace) nodraw
		
	* Combine graphs
		graph combine gr1 gr3 gr2 gr4, ycommon imargin(vsmall) iscale(*1.45) name(RefSupportoverTime, replace)
		
	
	
************************** Main regression analysis ****************************
	
***** Main regression analysis: Change in referendum support 2016 vs. 2015

	* Baseline regressions
	* with control for lagged level
	reg ref_diff16vs15 ref_Oct15 i.EU_w6
	eststo m1
	margins EU_w6, post
	eststo marg1
	
	* plus sociodemographic control variables
	reg ref_diff16vs15 ref_Oct15 i.EU_w6 i.incomecat i.agegroup i.educat i.east
	eststo m2
	margins EU_w6, post
	eststo marg2

	* plus vote intention
	reg ref_diff16vs15 ref_Oct15 i.EU_w6 i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
	eststo m3
	margins EU_w6, post
	eststo marg3

	* FIGURE 2: predicted probabilities for different values of EU integration attitude
	coefplot marg1 marg2 marg3, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
		legend(rows(1) ring(0) pos(12) size(small) order(3 "lagged level" 6 "+ sociodemographics" 9 "+ voting intention")) level(95 90) scale(*1.2) name(MainResults, replace)
		
	* write table with regression results for appendix   
	esttab m1 m2 m3 using Table_reg_main.rtf, b(a2) se(a2) star(* 0.05 ** 0.01 *** 0.001) scalars(r2) ///
		label nogaps  ///
		replace
	
	
***** Additional regression analysis: The conditioning effect of political involvement
	
	*** Conditioning on political knowledge
	reg ref_diff16vs15 ref_Oct15 i.EU_w6##i.polknowlegde_w7_dummy  i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
	eststo m1
	
	* predicted values at low political knowledge
	margins EU_w6, at(polknowlegde_w7_dummy=(0)) post
	eststo marg1

	* predicted values at high political knowledge
	estimates restore m1
	margins EU_w6, at(polknowlegde_w7_dummy=(1)) post
	eststo marg2
	
	* FIGURE 3: Predicted probabilities for different values of EU integration attitude by political involvement
	coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
		legend(rows(1) ring(0) pos(12) size(small) order(3 "political knowledge low" 6 "political knowledge high")) level(95 90) scale(*1.3)	name(ByPolKnowledge, replace)
		
	* write table with regression results for appendix   
	esttab m1 using Table_reg_polknow.rtf, b(a2) se(a2) star(* 0.05 ** 0.01 *** 0.001) scalars(r2) ///
		label nogaps  ///
		replace

				 
				 
******************************** Analyses for APPENDIX *********************************

*** APPENDIX C: Additionally controlling for attitudes towards immigration
	reg ref_diff16vs15 ref_Oct15 i.EU_w6 i.incomecat i.agegroup i.educat i.east ib1.voteint_w9 i.immigration_w9
	eststo m4
		
		corr immigration_w9 EU_w6 if e(sample)
	
	* create figure with predicted probabilities for different values of EU integration attitude
	margins EU_w6, post
	eststo marg4
	coefplot marg3 marg4, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
		legend(rows(1) ring(0) pos(12) size(small) order(3 "+ voting intention" 6 "+ immigration attitude")) level(95 90) scale(*1.2) name(AddImmigr, replace)

	* create figure with predicted probabilities for different values of immigration attitude
	estimates restore m4
	margins immigration_w9, post
	eststo marg5

	* create figure with predicted probabilities for different values of EU integration attitude
	coefplot marg5, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards immigration) level(95 90) scale(*1.2) name(AddImmigr_ByImmigr, replace)

	

*** APPENDIX D: Regressions with alternative cut-off points for political knowledge as a moderator 
	recode polknowlegde_w7 (0/6=0) (7 8 9=1), gen(polknowlegde_w7_dummy2)
	tab polknowlegde_w7_dummy2 polknowlegde_w7
			
	reg ref_diff16vs15 ref_Oct15 i.EU_w6##i.polknowlegde_w7_dummy2  i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
	eststo m1
			
	* predicted values at low political involvement
	margins EU_w6, at(polknowlegde_w7_dummy2=(0)) post
	eststo marg1

	* predicted values at high political involvement
	estimates restore m1
	margins EU_w6, at(polknowlegde_w7_dummy2=(1)) post
	eststo marg2
			
	coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
		legend(rows(1) ring(0) pos(12) size(small) order(3 "political knowledge low" 6 "political knowledge high")) level(95 90) scale(*1.3)	name(ByPolKnowledge_ALtCutoff, replace)
				
	* write table with regression results for appendix   
	esttab m1 using Table_reg_polknow2.rtf, b(a2) se(a2) star(* 0.05 ** 0.01 *** 0.001) scalars(r2) ///
		 label nogaps replace		
						 

*** APPENDIX E: Regressions with self-rated political interest as a moderator 
	reg ref_diff16vs15 ref_Oct15 i.EU_w6##i.polint_w9 i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
	eststo m1
			
	* predicted values at low political involvement
	margins EU_w6, at(polint_w9=(0)) post
	eststo marg1

	* predicted values at high political involvement
	estimates restore m1
	margins EU_w6, at(polint_w9=(1)) post
	eststo marg2
			
	coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
		legend(rows(1) ring(0) pos(12) size(small) order(3 "political interest low" 6 "political interest high")) level(95 90) scale(*1.3) name(ByPolInterest, replace)
				
	* write table with regression results for appendix   
	esttab m1 using Table_reg_polint.rtf, b(a2) se(a2) star(* 0.05 ** 0.01 *** 0.001) scalars(r2) ///
		label nogaps replace
	
	
*** APPENDIX E: Regressions with EU support measured in October 2016

	***** How stable is EU support? -> compare w6 (September 2013) to w10 (October 2016)
	tab EU_w10 EU_w6
	corr EU_w10 EU_w6	
	
	* Figure F1: First difference of EU support in October 2016 vs. September 2013
	gen EU_w6vsw10=EU_w10-EU_w6
	
	hist EU_w6vsw10, discrete percent xlabel(-6 (1) 6) addlabel xtitle(Difference in EU support: October 2013 vs. September 2016) name(EUsupport_stability, replace)
	fre EU_w6vsw10

	
	***** Repeat regressions with contemporaneous EU support
	
	***** Main regression analysis: Change in referendum support 2016 vs. 2015
		* Baseline regressions
		* with control for lagged level
		reg ref_diff16vs15 ref_Oct15 i.EU_w10
		eststo m1
		margins EU_w10, post
		eststo marg1
		
		* plus sociodemographic control variables
		reg ref_diff16vs15 ref_Oct15 i.EU_w10 i.incomecat i.agegroup i.educat i.east
		eststo m2
		margins EU_w10, post
		eststo marg2

		* plus vote intention
		reg ref_diff16vs15 ref_Oct15 i.EU_w10 i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
		eststo m3
		margins EU_w10, post
		eststo marg3

		* create figure with predicted probabilities for different values of EU integration attitude
		coefplot marg1 marg2 marg3, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
			legend(rows(1) ring(0) pos(12) size(small) order(3 "lagged level" 6 "+ sociodemographics" 9 "+ voting intention")) level(95 90) scale(*1.2) name(ContempEU_main, replace)

	
	***** Regression analysis: The conditioning effect of political involvement
		
		*** Conditioning on political knowledge
			reg ref_diff16vs15 ref_Oct15 i.EU_w10##i.polknowlegde_w7_dummy i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
			eststo m1
			
			* predicted values at low political involvement
			margins EU_w10, at(polknowlegde_w7_dummy=(0)) post
			eststo marg1

			* predicted values at high political involvement
			estimates restore m1
			margins EU_w10, at(polknowlegde_w7_dummy=(1)) post
			eststo marg2
			
			coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
				legend(rows(1) ring(0) pos(12) size(small) order(3 "political knowledge low" 6 "political knowledge high")) level(95 90) scale(*1.3)	name(ContempEU_byPolKnowledge, replace)


		
		*** Conditioning on political interest
			reg ref_diff16vs15 ref_Oct15 i.EU_w10##i.polint_w9 i.incomecat i.agegroup i.educat i.east ib1.voteint_w9
			eststo m1
			
			* predicted values at low political involvement
			margins EU_w10, at(polint_w9=(0)) post
			eststo marg1

			* predicted values at high political involvement
			estimates restore m1
			margins EU_w10, at(polint_w9=(1)) post
			eststo marg2
			
			coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
				legend(rows(1) ring(0) pos(12) size(small) order(3 "political interest low" 6 "political interest high")) level(95 90) scale(*1.3) name(ContempEU_byPolInterest, replace)


				
*** APPENDIX I: Regressions with direct democracy at the federal level as a moderator
	*** Is the effect stronger where people had less experience with referendums? -> use variation in number of referendums at the state level

	*** code number of referendums at the state level
		* according to: https://www.mehr-demokratie.de/fileadmin/pdf/volksentscheids-ranking_2016.pdf
		clonevar state=kp1_2601
		
		gen refnostate=0 if kp1_2601<.
		replace refnostate=7 if state==2
		replace refnostate=6 if state==9
		replace refnostate=5 if state==11
		replace refnostate=2 if state==1
		replace refnostate=1 if state==14
		replace refnostate=1 if state==13
		replace refnostate=1 if state==15
		
		* dummy 
		recode refnostate (0=0) (1/100=1), gen(refstate_dummy)
	
	
	*** subnational democracy index from Leemann & Stadelmann-Steffen (2022) -> https://journals.sagepub.com/doi/suppl/10.1177/00104140211024302
		gen snDDI=0 if kp1_2601<.
		
		replace snDDI=0.46 if state==1
		replace snDDI=0.44 if state==2
		replace snDDI=0.22 if state==3
		replace snDDI=0.34 if state==4
		replace snDDI=0 if state==5
		replace snDDI=0.67 if state==6
		replace snDDI=0 if state==7
		replace snDDI=0 if state==8
		replace snDDI=1.06 if state==9
		replace snDDI=0 if state==10
		replace snDDI=0.51 if state==11
		replace snDDI=0.56 if state==12
		replace snDDI=0.53 if state==13
		replace snDDI=0.2 if state==14
		replace snDDI=0 if state==15
		replace snDDI=0.48 if state==16

		* dummy 
		recode snDDI (0.0/0.39=0) (0.4/2=1), gen(snDDI_dummy)
		
		* compare the two measures
		preserve
		collapse (mean) refstate_dummy snDDI_dummy, by(state) 
		tab refstate_dummy snDDI_dummy
		restore
		

		* multilevel regression with dummy for referendums at the state level
		mixed ref_diff16vs15 ref_Oct15 c.EU_w6##i.refstate_dummy i.incomecat i.agegroup i.educat i.east ib1.voteint_w9 || state: c.EU_w6
			eststo m1
	
		margins, at(EU_w6=(0 (1) 6) refstate_dummy=(0)) post
			eststo marg1

		estimates restore m1
		margins, at(EU_w6=(0 (1) 6) refstate_dummy=(1)) post
			eststo marg2

		coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
			coeflabel(1._at="Less EU integration" 2._at="1" 3._at="2" 4._at="3" 5._at="4" 6._at="5" 7._at="More EU integration") ///
			legend(rows(1) ring(0) pos(12) size(small) order(3 "state without referendum" 6 "state with referendum(s)")) level(95 90) scale(*1.3) name(ByStateReferendums, replace)	
			
			
		* multilevel regression with dummy for direct democracy index at the state level
		mixed ref_diff16vs15 ref_Oct15 c.EU_w6##i.snDDI_dummy i.incomecat i.agegroup i.educat i.east ib1.voteint_w9 || state: c.EU_w6
			eststo m1
	
		margins, at(EU_w6=(0 (1) 6) snDDI_dummy=(0)) post
			eststo marg1

		estimates restore m1
		margins, at(EU_w6=(0 (1) 6) snDDI_dummy=(1)) post
			eststo marg2

		coefplot marg1 marg2, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Change in referendum support) xtitle(Attitude towards EU integration) ///
			coeflabel(1._at="Less EU integration" 2._at="1" 3._at="2" 4._at="3" 5._at="4" 6._at="5" 7._at="More EU integration") ///
			legend(rows(1) ring(0) pos(12) size(small) order(3 "state with low direct democracy" 6 "state with high direct democracy")) level(95 90) scale(*1.3)	name(ByStateDirDem, replace)	

			

*** APPENDIX G: Alternative fixed effect panel regression analysis
	* As this is a fixed effect panel model, data needs to brought into "long" panel structure first

	*** Transform data from wide into long format (i.e. panel structure)
		* Rename variables without a wave assignment
		rename kpx_* a_*

		* write wave number at end of variable names for other variables
		foreach i of numlist 18/1 {
			rename kp`i'* v*_`i'
			}
			
		* local of wave variables to keep
		local list "v_2880e_*  v_1250_* v_190ba_* v_2320_*"

		* keep variables
		keep study-a_2290 `list'

		* reshape long
		quietly reshape long v_2880e_ v_1250_ v_190ba_ v_2320_ ///
			, i(lfdn) j(welle)	
			
		* define value labels for wave variable	
		label define welle 1 "June 2013" 2 "July 2013" 3 "August 2013 (1)" 4 "August 2013 (2)" 5 "September 2013" /// 
			6 "September 2013" 7 "October 2013" 8 "October 2014" 9 "October 2015" 10 "October 2016" ///
			11 "February 2017" 12 "May 2017" 13 "July 2017" 14 "August 2017" 15 "September 2017 (1)" ///
			16 "September 2017 (2)" 17 "October 2017" 18 "March 2018"  	
		label values welle welle		
	
		* declare data as panel data 
		xtset lfdn welle

		
	*** Recode variables
		* create before/after Brexit dummy 
		recode welle (1/9=0 "before Brexit") (10/18=1 "after Brexit"), gen(PostBrexit)

		* create new wave variable including only the waves in which the referendum question was included
		recode welle (2=1 "July 2013") (5=2 "Sept 2013") (8=3 "Oct 2014") (9=4 "Oct 2015") (10=5 "Oct 2016") ///
			(13=6 "July 2017") (16=7 "Sept 2017") (1 3 4 6 7 11 12 14 15 17 18=.), gen(availablewaves)
		
		* recode referendum variable
		recode v_2880e_ (1=0 "disagree") (2=1 "tend to disagree") (3=2 "neither") (4=3 "tend to agree") (5=4 "agree"), gen(referendum)

		* recode EU attitude 
		recode v_1250_ (1=6 "More EU integration") (2=5) (3=4) (4=3) (5=2) (6=1) (7=0 "Less EU integration"), gen(EU)

			recode EU (6 5 4=2 "More EU integration") (3=1 "Middle") (2 1 0=0 "Less EU integration"), gen(EU_trich)
			
			* replace EU attitude with previous values if missing in wave
			sort lfdn welle
			replace EU=EU[_n-1] if EU>=.
			* replace EU attitude with later values if missing in wave
			replace EU=EU[_n+1] if EU>=.

		* recode voting intention
		clonevar partyvote=v_190ba_
			label define kp18_190ba 7 "THE LEFT", modify
			label define kp18_190ba 6 "GREENS", modify
		
		* recode education
			fre v_2320_
			recode v_2320_ (1 2 = 1) (3 = 2) (4 5 = 3) (9 = .), gen(educat)
			capture label drop educat
			label define educat ///
			1 "education low" ///
			2 "education middle"  ///
			3 "education high"
			label values educat educat		
						
			* replace education with previous values if missing in wave
			sort lfdn welle
			replace educat=educat[_n-1] if educat>=.
			* replace education with later values if missing in wave
			replace educat=educat[_n+1] if educat>=.

		
	*** Run regressions
	
	*** Estimate fixed effect model with one post-Brexit referendum dummy
		xtreg referendum i.PostBrexit##i.EU, fe
		eststo m1
		
			* write table with regression results   
			esttab m1 using Table_reg_FE1.rtf, b(a2) se(a2) star(* 0.05 ** 0.01 *** 0.001) scalars(r2) ///
				 label nogaps  ///
				 replace
			
	* marginal effect of post-Brexit referendum dummy
		margins, dydx(PostBrexit) at(EU=(0/6)) post
		eststo marg1
		
		coefplot marg1, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Effect of post-Brexit referendum dummy) xtitle(Attitude towards EU integration) ///
			coeflabel(1._at="Less EU integration" 2._at="1" 3._at="2" 4._at="3" 5._at="4" 6._at="5" 7._at="More EU integration") ///
			level(95 90) scale(*1.3) name(FE_BrexitrefDummy, replace)

			
	*** Estimate fixed effect model with dummies for all waves 
		xtreg referendum i.availablewaves##i.EU, fe
			eststo m2
			
			* write table with regression results   
			esttab m2 using Table_reg_FE2.rtf, b(a2) se(a2) star(* 0.05 ** 0.01 *** 0.001) scalars(r2) ///
				 label nogaps  ///
				 replace
				
		* compute and plot marginal effects of post-Brexit waves conditional on EU attitude 		
		margins, dydx(5.availablewaves) at(EU=(0/6)) post
		eststo marg1
		
		estimates restore m2
		margins, dydx(6.availablewaves) at(EU=(0/6)) post
		eststo marg2
		
		estimates restore m2
		margins, dydx(7.availablewaves) at(EU=(0/6)) post
		eststo marg3
				
		coefplot marg1 marg2 marg3, vertical xlabel(, angle(045)) yline(0) title(" ") ytitle(Effect of post-Brexit referendum dummy) xtitle(Attitude towards EU integration) ///
			coeflabel(1._at="Less EU integration" 2._at="1" 3._at="2" 4._at="3" 5._at="4" 6._at="5" 7._at="More EU integration") ///
			legend(rows(1) ring(0) pos(6) size(small) order(3 "October 2016" 6 "July 2017" 9 "October 2017")) ///
			ylabel(-.75 (.25) .25) ///
			level(95 90) scale(*1.15) name(FE_PostBrexitrefWaves, replace)
		
		* compute and plot predicted values for all waves conditional on EU attitude 		
		estimates restore m2
		margins, at(availablewaves=(1/7) EU=(0/6)) post
		
		mplotoffset, xtitle(" ") xlabel(, angle(045)) legend(rows(1) ring(0) pos(6) size(small)) ///
			 scale(*1.15) name(FE_PredictedAllWaves, replace)	
			
	
	
*** APPENDIX H: Referendum support in further subgroups over time			
	*** Figure H1: Referendum support by voting intention
	lgraph referendum availablewaves if partyvote<800, by(partyvote) errortype(ci(95)) ///
			ytitle(referendum support) xtitle(" ") ///
			xlabel(1 "July 2013" 2 "Sept 2013" 3 "Oct 2014" 4 "Oct 2015" 5 "Oct 2016" 6 "July 2017" 7 "Sept 2017", angle(045)) ///				
			separate(0.01) ///
			xline(4.5, lcolor(gs10)) scale(*1.2) name(RefSupport_byvote, replace)	
			
			
	*** Figure H2: Referendum support by education
	lgraph referendum availablewaves, by(educat) errortype(ci(95)) ///
			ytitle(referendum support) xtitle(" ") ///
			xlabel(1 "July 2013" 2 "Sept 2013" 3 "Oct 2014" 4 "Oct 2015" 5 "Oct 2016" 6 "July 2017" 7 "Sept 2017", angle(045)) ///				
			separate(0.01) ///
			xline(4.5, lcolor(gs10)) scale(*1.2) name(RefSupport_byeduc, replace)	
